<template>
  <div><p>登录成功，本页面将在{{ second }}秒钟后自动关闭，请稍后...</p></div>
</template>

<script>
import {dateFormat} from "@/utils/utils";
import {mapActions, mapGetters} from "vuex";
import {getUserInfo} from "@/api/user";
import {setQQLoginFlag} from "@/api/auth";

export default {
  name: "QQLogin",
  data() {
    return {
      second: 3
    }
  },
  computed: {
    ...mapGetters(['getToken', 'getUser']),
  },
  methods: {
    ...mapActions(['setToken', 'setUser', 'setAuth', 'setQQLogin']),
    closeTab() {
      window.opener = null;
      window.open("about:blank", "_top").close()
    },
    countDown() {
      let code = setInterval(() => {
        this.second--;
        if (this.second == 0) {
          this.closeTab()
          clearInterval(code)
          this.setQQLogin(true)
        }
      }, 1000)
    },
    getInfoData() {
      let url = window.document.location.href.toString();//获得当前url地址并转换成字符串
      let u = url.split("?");//以？来分割字符串，将？前后的值存到一个数组中
      if (typeof (u[1]) == "string") {//获得？后面具体的请求参数值
        u = u[1].split("&");
        let result = {};
        u.forEach(item => {
          let temp = item.split("=")
          result[temp[0]] = temp[1];
        })
        return result;
      }
    }
  },
  created() {
    let infoData = this.getInfoData();
    getUserInfo(infoData.uid).then(res => {
      res.data.birthday = res.data.birthday ? dateFormat(new Date(res.data.birthday), 'yyyy-MM-dd HH:mm:ss') : res.data.birthday;
      const token = infoData.token;
      const auth = infoData.authorities;
      this.setToken(token);
      this.setUser(res.data);
      this.setAuth(auth);
      this.countDown();
      setQQLoginFlag(res.data.id).then(res => {
        // console.log(res)
      })
    })
  }
}
</script>

<style scoped>

</style>
